<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="`${info.reply_count}条回复`">
      <van-icon slot="left" name="cross" @click="$emit('offReply')" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <div class="main">
      <!-- 当前评论 -->
      <van-cell title="当前评论">
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="info.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ info.aut_name }}</span>
        <div slot="label">
          <p class="comment-content" style="color: #363636;">{{ info.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ info.pubdate | relativeTime }}</span>
            <van-button
              size="mini"
              type="default"
            >回复 {{ info.reply_count }}</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" />
      </van-cell>
      <!-- /当前评论 -->
      <van-divider>全部回复</van-divider>

      <!-- 对当前评论 回复列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="item in list"
          :key="item.com_id.toString()"
        >
          <van-image
            slot="icon"
            round
            width="30"
            height="30"
            style="margin-right: 10px;"
            :src="item.aut_photo"
          />
          <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
          <div slot="label">
            <p class="reply-content" style="color: #363636;">{{ item.content }}</p>
            <p>
              <span style="margin-right: 10px;">{{ item.pubdate | relativeTime }}</span>
            </p>
          </div>
        </van-cell>
      </van-list>
    </div>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="commentText"
        clearable
        placeholder="请输入回复内容"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="handlePutComment"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>

</template>
<script>
import { getComments, putComment } from '@/api/articles'
export default {
  name: 'CommentReply',
  props: {
    info: {
      type: Object,
      required: true
    },
    artid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      commentText: ''
    }
  },
  methods: {
    //   加载评论
    async  onLoad() {
      if (this.info.reply_count === 0) {
        this.finished = true
        return
      }
      try {
        const res = await getComments('c', this.info.com_id.toString(), this.offset, 10)
        this.offset = res.data.last_id
        this.list = [...this.list, ...res.data.results]
        this.loading = false
        if (this.offset <= res.data.end_id) this.finished = true
      } catch (error) {
        this.$toast.fail('获取回复失败!')
      }
    },

    // 发布评论
    async handlePutComment() {
      try {
        const res = await putComment(this.info.com_id.toString(), this.commentText, this.artid)
        this.list.unshift(res.data.new_obj)
        this.info.reply_count++
        this.$toast.success('发送成功')
        this.commentText = ''
      } catch (error) {
        this.$toast.fail('发送失败')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// 固定头部和限制滚动条区域
.article-comments {
 display: flex;
 flex-direction: column;
 height: 100%;
 .van-nav-bar__content {
     height: 46px;
 }
 .main {
     flex:1;
     overflow: scroll;
 }
}
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
.comment-content,.reply-content {
    word-break:break-all
}
</style>
